package com.tantou.leetcode.easy;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jiangweiyang
 * @created 2020/4/29 19:07
 **/
public class TwoNumSum {

    /**
     * https://leetcode-cn.com/problems/two-sum/
     * 两数之和
     * 给定一个整数数组和一个目标值target，请你在该数组中找出和为目标值的两个整数，并返回他们的数组下标
     * 数组中同一个元素不能使用两遍
     *
     * @param args
     */
    public static void main(String[] args) {
        int nums[] = {3, 3};
        int[] violenceResult = violence(nums, 9);
        System.out.println(Arrays.toString(violenceResult));

        int[] hashResult = hash(nums, 46);
        System.out.println(Arrays.toString(hashResult));

        int[] oneHashResult = oneHash(nums, 6);
        System.out.println(Arrays.toString(oneHashResult));
    }

    /**
     * 时间复杂度较高，O(n^2)
     */
    public static int[] violence(int[] nums, int target) {
        if (nums == null && nums.length == 0) {
            throw new RuntimeException("XXX");
        }
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (target - nums[j] == nums[i]) {
                    return new int[]{i, j};
                }
            }
        }
        return null;
    }

    /**
     * hash法，
     * 复杂度 O(1)
     */
    public static int[] hash(int[] nums, int target) {
        /**
         * Key:num
         * Value:Index
         */
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }

        for (int i = 0; i < nums.length; i++) {
            int cur = nums[i];
            int compare = target - cur;
            /**
             * 找出存在map的值，且两个元素不是同一个
             */
            if (map.containsKey(compare) && map.get(compare) != i) {
                return new int[]{i, map.get(compare)};
            }
        }
        return null;
    }

    /**
     * hash法，
     * 复杂度 O(1)
     */
    public static int[] oneHash(int[] nums, int target) {
        /**
         * Key:num
         * Value:Index
         */
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            int cur = nums[i];
            int compare = target - cur;
            /**
             * 需要在放进去之前，跟已存进去的校验
             * 如果放进去之后再拿出来，则两个比较的很可能是同一个数
             * 找出存在map的值，且两个元素不是同一个
             */
            if (map.containsKey(compare) && map.get(compare)!=i) {
                return new int[]{map.get(compare), i};
            }
            map.put(nums[i], i);
        }
        return null;
    }


}

class Solution {
    public int[] sum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[] {map.get(target - nums[i]), i};
            }
            map.put(nums[i], i);
        }
        return new int[0];
    }
}
